package com.hifar.test.equipsensor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.test.equipsensor.mapper.HfResEquipSensorMapper;
import com.hifar.test.equipsensor.pojo.EquipClassifySearchVo;
import com.hifar.test.equipsensor.pojo.EquipSensorSearchVo;
import com.hifar.test.equipsensor.pojo.HfResEquipSensor;
import com.hifar.test.equipsensor.service.IHfResEquipSensorService;
import com.hifar.test.sensor.pojo.HfResSensor;
import com.hifar.test.sensor.service.IHfResSensorService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源-设备传感器-关系表 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-11 14:13:02
 */
@Service
public class HfResEquipSensorServiceImpl extends ServiceImpl<HfResEquipSensorMapper, HfResEquipSensor> implements IHfResEquipSensorService {

    @Autowired
    private IHfResSensorService resSensorService;

    @Override
    public BusiResponse exportExcel(Map<String, Object> pageInfo) {
        BusiResponse busiResponse = new BusiResponse();
        String ids = (String) pageInfo.get("ids");

        String operationType = (String) pageInfo.get("operationType");

        if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
            ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResEquipSensor.class, "导入模板", "资源-设备传感器-关系表导入数据");
            return busiResponse;
        } else {
            QueryWrapper<HfResEquipSensor> postParam = RequestConver.getPostParam(pageInfo);
            postParam.ne("del_flag", 1);
            postParam.orderByDesc("create_time");
            if (StringUtils.isNotBlank(ids)) {
                postParam.in("id", ids);
            }
            List<HfResEquipSensor> dataList = this.list(postParam);
            if (dataList != null && dataList.size() > 0) {
                ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfResEquipSensor.class, "资源-设备传感器-关系表数据", "资源-设备传感器-关系表导出");
            } else {
                ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResEquipSensor.class, "资源-设备传感器-关系表数据", "资源-设备传感器-关系表导出");
            }
            return busiResponse;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public BusiResponse importExcel(MultipartFile file) throws Exception {
        if (file != null && !file.isEmpty()) {
            List<HfResEquipSensor> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), HfResEquipSensor.class);
            List<HfResEquipSensor> importExcelList = new ArrayList<HfResEquipSensor>();
            if (importExcel != null && importExcel.size() > 0) {
                for (HfResEquipSensor bean : importExcel) {
                    bean.setId(IdUtils.uuid32());
                    //if(!bean.getName().equals("")) {
                    importExcelList.add(bean);
                    //}
                }
                this.saveBatch(importExcelList);
                return BusiResponse.ok("导入成功！");
            }
        }
        return BusiResponse.error("导入失败，原因未知！");
    }

    @Override
    public Page<EquipSensorSearchVo> entityVoPage(Page<EquipSensorSearchVo> rowPage, QueryWrapper<EquipSensorSearchVo> wrapper) {
        return this.getBaseMapper().entityVoPage(rowPage, wrapper);
    }

    @Override
    public List<EquipSensorSearchVo> listEntityVo(QueryWrapper<EquipSensorSearchVo> wrapper) {
        return this.getBaseMapper().listEntityVo(wrapper);
    }

    @Override
    public Page<EquipClassifySearchVo> entityEquipClassifyVoPage(Page<EquipClassifySearchVo> rowPage, QueryWrapper<EquipClassifySearchVo> wrapper) {
        return this.getBaseMapper().entityEquipClassifyVoPage(rowPage, wrapper);
    }

    @Override
    public List<EquipClassifySearchVo> listEntityEquipClassifyVo(QueryWrapper<EquipClassifySearchVo> wrapper) {
        return this.getBaseMapper().listEntityEquipClassifyVo(wrapper);
    }

    @Override
    public List<HfResSensor> listSensorByEquipId(String equipId) {
        QueryWrapper<HfResEquipSensor> equipSensorQueryWrapper = new QueryWrapper<>();
        equipSensorQueryWrapper.lambda()
                .eq(HfResEquipSensor::getEquipId, equipId)
                .orderByAsc(HfResEquipSensor::getRowSort);
        List<HfResEquipSensor> equipSensorList = this.list(equipSensorQueryWrapper);
        List<String> sensorIdList = equipSensorList.stream()
                .map(HfResEquipSensor::getSensorId)
                .collect(Collectors.toList());
        if (sensorIdList.isEmpty()) {
            return new ArrayList<>(0);
        }
        return resSensorService.listByIds(sensorIdList);
    }
}
